package com.lgqz.himalayafm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.bean.AnchorRecyclerEntity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2016/12/6.
 */

public class AnchorRecyclerAdapter extends RecyclerView.Adapter<AnchorRecyclerAdapter.ViewHolder> {

    private Context context;
    private List<AnchorRecyclerEntity.AnchorCategoriesData> list = new ArrayList<>();
    private RecyclerView recyclerView;

    public AnchorRecyclerAdapter(Context context) {
        this.context = context;


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.anchor_recycler_item, parent, false);

        final ViewHolder holder = new ViewHolder(view);
        holder.button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (recyclerView != null) {
                    holder.button1.setText("");
                    holder.button1.setCompoundDrawables(null, null, null, null);
                    holder.button1.setBackgroundResource(R.drawable.cancel_focus_btn_pressed);
                }
            }
        });
        return holder;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;

    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Log.d("1620", "onBindViewHolder: " + 1);
        if (list.get(position).getTitle().equals("二次元")) {

        } else {
            holder.cateButton.setText(list.get(position).getTitle());
            holder.simpleDraweeView1.setImageURI(list.get(position).getList().get(0).getSmallLogo());
            Log.d("1620", "onBindViewHolder: " + 2);
            holder.textView1.setText(list.get(position).getList().get(0).getNickname());
            holder.simpleDraweeView2.setImageURI(list.get(position).getList().get(1).getSmallLogo());
            holder.textView2.setText(list.get(position).getList().get(1).getNickname());
            holder.simpleDraweeView3.setImageURI(list.get(position).getList().get(2).getSmallLogo());
            holder.textView3.setText(list.get(position).getList().get(2).getNickname());

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public void addAll(Collection<? extends AnchorRecyclerEntity.AnchorCategoriesData> collection) {
        if (collection.size() != 0) {
            Log.d("1620", "addAll: " + collection.size());
            int size = list.size();
            list.addAll(collection);
            notifyItemRangeInserted(size, collection.size());
        }
    }

    public void clearData() {
        list.clear();
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private SimpleDraweeView simpleDraweeView1, simpleDraweeView2, simpleDraweeView3;
        private TextView textView1, textView2, textView3;
        private Button button1, cateButton;

        public ViewHolder(View itemView) {
            super(itemView);
            simpleDraweeView1 = (SimpleDraweeView) itemView.findViewById(R.id.anchor_recycler_item_image1);
            simpleDraweeView2 = (SimpleDraweeView) itemView.findViewById(R.id.anchor_recycler_item_image2);
            simpleDraweeView3 = (SimpleDraweeView) itemView.findViewById(R.id.anchor_recycler_item_image3);
            textView1 = (TextView) itemView.findViewById(R.id.anchor_recycler_item_tv1);
            textView2 = (TextView) itemView.findViewById(R.id.anchor_recycler_item_tv2);
            textView3 = (TextView) itemView.findViewById(R.id.anchor_recycler_item_tv3);
            cateButton = (Button) itemView.findViewById(R.id.anchor_recycler_item_cate_btn);
            button1 = (Button) itemView.findViewById(R.id.anchor_recycler_item_btn1);

        }
    }
}
